/*
	修改时间：2010.06.04
	说明：优惠礼包详细页面操作相关函数	

	数据格式：
	{
		id:礼包ID,
		name:礼包名称,
		qn:礼包商品数量,
		price:礼包价格,
		items:{
			 "礼包商品组ID":  {
				   id:商品ID,
				   oaid:商品规格ID,
				   name:商品名称,
				   price:商品原本站价,
				   picaddr:商品缩略图,
				   packed:是否已选择了商品,
				   specitems:[
						id:规格子项ID,
						name:规格子项名称,
						value:规格子项值
					]选中的商品规格块(当商品有规格时)
			   }
		}礼品商品列表
	}

*/

var lodo_make_info=[];//礼包加载数据;

var make_goods_spec_json=[];
var lodo_make_goods_sel_info={};

make_cls=function(){
	var that=this;
	var O=new cart_r_cls();
	
	//要操作用到的DOM对象
	var sellist=$("#make_selection_list");//礼包框列表块
	var goodslist=$("#make_goods_info");//商品列表块
	var goodsinfo_div=$("#make_tc_goods_info");//弹出选择商品块
	
	var addCartno=false;//是否可加入购物车
	var finded=false;//是否还有未选择的商品礼包
	var goodsinfono=false;//是否弹出了商品详细块信息
	var item_id=-1;//当前要选择的礼包商品
	
	//礼包信息
	var make_id=lodo_make_info.id;//礼包ID
	var make_name=lodo_make_info.name;//礼包名称
	var make_price=lodo_make_info.price;//礼包价格
	var make_goods_num=lodo_make_info.qn;//礼包商品件数
	
	
	//检查礼包数量
	if(make_goods_num<=0){
		that.errMsg(lodo_lang_js.make.tip_1);
	}else{
		item_id=1;
	}
	
	
	//出错提示
	this.errMsg=function(msg){
		goodslist.html(msg);
	}
	
	//搜索下个没有选择商品的礼包
	this.findItem=function(){
		for(var i=1;i<=make_goods_num;i++){
			if(!lodo_make_info.items[i].packed){
				item_id=i;
				finded=false;
				return;
			}
		}
		finded=true;		
	}
	
	this.pickItem=function(obj,num){
		if(goodsinfono){return;}
		if(num>0&&num<=make_goods_num){
			if(item_id!=num){
				item_id=num;
				that.loadgoodslist();
			}
			
		}
	}

	//关闭商品详细页面
	this.closegoods=function(){
		$.fancybox.close();
	}
	
	//加载商品列表
	this.loadgoodslist=function(){
		var url=lodo_web_path+"ajax_make.asp?act=goodslist&mid="+make_id+"&groupid="+item_id+"&bn="+Math.random();
		goodslist.load(url);
	}
	
	//将商品添加至选择列表中
	this.addToMakeList=function(){
		if($.isEmptyObject(lodo_make_goods_sel_info)){alert(lodo_lang_js.make.tip_2);return false;}
		lodo_make_info.items[item_id]=lodo_make_goods_sel_info;
		that.closegoods();
		that.buildSelectionList(item_id);
		that.findItem();
		that.loadgoodslist();
	}
	
	this.removeFromPackage=function(tmpid){
		lodo_make_info.items[tmpid]={
			id:0,
		   oaid:0,
		   name:"",
		   price:"",
		   picaddr:"",
		   packed:false,
		   specitems:[]
		}
		finded=false;
		item_id=tmpid;
		that.buildSelectionList(tmpid);
		that.loadgoodslist();
	}
	
	//将已选择的商品信息显示出来
	this.buildSelectionList=function(I){
		
		var make_name=$("#make_"+I+"_name");
		var make_picaddr=$("#make_"+I+"_picaddr");
		var make_price=$("#make_"+I+"_price");
		var make_spec_list=$("#make_"+I+"_spec_list");
		var items=lodo_make_info.items[I];
		if(items){
			var gname=make_name.attr("oldmname");
			var gpicaddr=make_picaddr.attr("oldmsrc");
			var gprice=make_price.attr("oldmprice");
			var gspec_list="";
			if(items.packed){
				gname=items.name;
				gpicaddr=items.picaddr;
				gprice=items.price;
				$(items.specitems).each(function(){
					gspec_list+="<li>"+this.name+":"+this.value+"</li>"
				});
				if(gspec_list!=""){gspec_list="<ul>"+gspec_list+"</ul>"}
			}
			make_name.html(gname);
			make_picaddr.attr("src",gpicaddr);
			make_price.html(gprice);
			make_spec_list.html(gspec_list);
		}

	}
	//加入购物车
	this.addToCart=function(obj){
		//判断是否都选择了
		if(addCartno){return;}
		if(!finded){addCartno=false;alert(lodo_lang_js.make.tip_3);return;}
		addCartno=true;
		var url=lodo_web_path+"addmaketocart.asp?act=addmake&mgid="+lodo_make_info.id+"";
		for(var I in lodo_make_info.items){
			var items=lodo_make_info.items[I];
			var gid=items.id;
			var oaid=items.oaid;
			url+="&gid_"+I+"="+gid+"&oaid_"+I+"="+oaid+""
		}
		O.show($(obj));
		O.reportFailed("<img src='images/loading3.gif'>"+lodo_lang_js.make.tip_4);
		
		var option={
				action:function(){
					$.getJSON(url,function(data){
						addCartno=false;
						O.hide();
						if(data.errno){alert(data.msg);return false;}
						if(data.gotono){location.href=data.gotourl};
						if(data.logno){
							$loginFrame({
								type:"func",
								check: false,
								checkReady:false,
								model: false,
								title:lodo_lang_js.make.tip_5,
								gotourl:data.gotourl,
								action:option.action
							});
							return false;
						}
						if(data.cartno){
								O.show($(obj),{isReportSuccess:"true"});
								O.reportSuccess(data);
								$("#ark_continueShopping,#ark_closeIndicator").click(function(){
									  O.hide();
									  return false;
								  });
							return false;
						}
					})
				}
			};
			option.action();
	}
	
	//初始加载信息
	init=function(){
		that.loadgoodslist();
	}
	init();
}




//规格
var lodo_make_Meal_list={};
var make_gid=0;
var cartbtnsumitName;
var productName="";
//事件处理
var Make_Meal=(function(){
	   //var f="\u6B63\u5728\u6DFB\u52A0\u4F18\u60E0\u5957\u9910\u5230\u8D2D\u7269\u8F66...\u8BF7\u7A0D\u5019";//正在添加优惠套餐到购物车...请稍候
	   //var lang_MAXQN="\u5BF9\u4E0D\u8D77\uFF0C\u8BE5\u5546\u54C1\u53EF\u9009\u7684\u6700\u5927\u6570\u91CF\u4E3A";//对不起，该商品可选的最大数量为
	   
	   //var P="\u5546\u54c1\u5df2\u6dfb\u52a0\u5230\u8d2d\u7269\u8f66\uff01";//商品已添加到购物车！
	   //var lang_PJ="\u8BF7\u9009\u62E9\u914D\u4EF6\uFF1A";//请选择配件
	   //var lang_PS="\u914D\u4EF6\uFF1A\"{name}\"\u6570\u91CF\u53EA\u80FD\u5728{minnum}\u548C{maxnum}\u4E4B\u95F4";//配件："{name}"数量只能在{minnum}和{maxnum}之间!
	   //var lang_lv="\u8D2D\u4E70\u6570\u91CF\u53EA\u80FD\u662F\u4E00\u624B\u7684\u6574\u6570\u500D";//购买数量只能是一手的整数倍！

	   var Q=lodo_lang_js.make.tip_6;//此商品缺货中！
	   var N=lodo_lang_js.make.tip_7;//请选择：
	   var H=lodo_lang_js.make.tip_9;//您所填写的商品数量超过库存！
	   var K=lodo_lang_js.make.tip_10;//请填写正确的商品数量！
	   var U=lodo_lang_js.make.tip_11;//您是卖家,不能添加到购物车！
	var O=new cart_r_cls();
					   
   var I=function(g){
	   O.show($(cartbtnsumitName));
	   O.reportFailed(g)
	  };
   var E=function(){
		O.hide();
		$(cartbtnsumitName).show();
	   var j=$("#ark_listCart");
	   if(!j[0]){I(U);return false}
	   if(typeof(sellProperty[make_gid])!="undefined"){
		   if(!sellProperty[make_gid].isAllPVSelected()){
			   var k=sellProperty[make_gid].getUnselectedPVNames().join('", "');
			   I(N+' "'+k+'"');
			   $("#make_"+make_gid+"_info-hx").html("<em>"+N+' "'+k+'"</em>');
			   return false
			}
		}
		var m=$("#make_"+make_gid+"_buy_maxCount");
		var l=$("#make_"+make_gid+"_buyqn");
		var buy_minqn=$("#make_"+make_gid+"_minbuyqn");
	    var buy_maxqn=$("#make_"+make_gid+"_maxbuyqn");
		if(!m[0]){return false}
		if(!l[0]){return false}
		if(!buy_minqn[0]){return false}
		if(!buy_maxqn[0]){return false}
		var i=/^[1-9]\d*$/;
		if(!i.test(l.val().replace(/(^\s*)|(\s*$)/g,""))){
			I(K);
			return false
			}
		var h=parseInt(l.val());
		var g=parseInt(m.val());
		var bminqn=parseInt(buy_minqn.val());
		var bmaxqn=parseInt(buy_maxqn.val());
		//var lv=l.attr("lv");
		if(g==0){I(Q); return false;}
		
/*		if ( h<=0 || (h % lv) != 0)
		{
			I(lang_lv);
			return false;
		}*/
		if(h>g&&g!=-1){I(H);return false}
		
		if(!$.isEmptyObject(lodo_make_Meal_list[make_gid])){h+=(lodo_make_Meal_list[make_gid].buyqn);}
		//判断是否已经选择了,如果选择了判断是否已经是最大值
		if(h<bminqn){I(lodo_lang_js.make.tip_12.replace("{$1}",bminqn));return false}
		if(h>bmaxqn){I(lodo_lang_js.make.tip_13.replace("{$1}",bmaxqn));return false}
		var buyqn_total=h;
		for(var B in lodo_make_Meal_list)
		{
			if(!$.isEmptyObject(lodo_make_Meal_list[B])&&make_gid!=B){
				buyqn_total+=lodo_make_Meal_list[B].buyqn;
			}
		}
		var make_buyQn_Total=$("#make_buyQn_Total").val()*1;
		if(buyqn_total>make_buyQn_Total){I(lodo_lang_js.make.tip_14.replace("{$1}",make_buyQn_Total));return false}
		
		return true
		};
	var a={};
	a.attachEventToAddTrigger=function(){
		$("div[id$='_sellProperty_area']").hover(
										  function(){
												$(this).addClass("no-selceted");
												return false;
											},
										  function(){
												$(this).removeClass("no-selceted");
												return false;
		})
		
		$(".btn_addToMake").bind("click",function(){
									var make_gid=$(this).attr("gid");
								   var k=$("#make_"+make_gid+"_buy_itemId").val();
								   var g=$("#make_"+make_gid+"_buy_itemDetailId").val();
								   var i=$("#make_"+make_gid+"_buyqn").val();
									cartbtnsumitName=this;
								   if(!!E()){
									   addtomake(k,g,i);
									}
									return false;
				})
				.bind("mouseover",function(){
									make_gid=$(this).attr("gid");
									cartbtnsumitName=this;
									E();//需重复两次才起作用
									if(!E()){
										$(this).css("cursor","not-allowed");
									}else{
										$(this).css("cursor","");
										if($("#make_addToMakeIndicator")[0]){O.hide()}
									}
				})
				.bind("mouseout",function(){
								make_gid=$(this).attr("gid");
								cartbtnsumitName=this;
							   $(this).css("cursor","");
							   O.hide();
				});
		
		
		$(".baddtocart").bind("click",function(){
			cartbtnsumitName=this;
			var make_id=$(this).attr("bgid");
			var url="";
			var btotalNum=0;
			var buyqn_total=0;
			for(var I in lodo_make_Meal_list)
			{
				var tmp_meal=lodo_make_Meal_list[I];
				if(!$.isEmptyObject(tmp_meal)){
					btotalNum++;
					if(url!=""){url+="&"}
					url+="gid_"+btotalNum+"="+tmp_meal.gid;
					url+="&oaid_"+btotalNum+"="+tmp_meal.oaid;
					url+="&buyqn_"+btotalNum+"="+tmp_meal.buyqn;
					buyqn_total+=tmp_meal.buyqn;
				}
			}
			var make_buyQn_Total=$("#make_buyQn_Total").val()*1;
			if(buyqn_total!=make_buyQn_Total){alert(lodo_lang_js.make.tip_15.replace("{$1}",make_buyQn_Total));return false;}
			if(url!=""){url="&"+url;}
			url=lodo_web_path+"addbindgoodstocart.asp?act=add&gid="+make_id+url;
			a.detachEventFromAddTrigger();
			$.getJSON(url,function(data){
				O.hide();
				$(cartbtnsumitName).show();
				if(data.errno){alert(data.msg);return false;}
				if(data.gotono){location.href=data.gotourl};
				if(data.cartno){
						O.show($(cartbtnsumitName),{isReportSuccess:"true"});
						O.reportSuccess(data);
						a.attachEventToAddTrigger();
						$("#ark_continueShopping,#ark_closeIndicator").live("click",function(){
																  O.hide();
																  return false;
																  });
				}
			})
			
		})

	};
	a.detachEventFromAddTrigger=function(){
		$(".btn_addToMake,.baddtocart").unbind("click");
		//$(".btn_addToMake,.baddtocart").click(function(){return false;});
		O.show($(cartbtnsumitName));
		O.reportFailed("<img src='images/loading3.gif'>"+lodo_lang_js.make.tip_4);
		$(cartbtnsumitName).hide();
	};
	a._init=function(){
		this.attachEventToAddTrigger();
		};
	a._init();
	return a  
});


function load_make_goods_spec(){
	sellProperty={};
	$.each(make_goods_spec_json,function(i,items){
		var sellProps=items["sellProps"];
		var allItems=items["allItems"];
		var mealDetailPrice = [];
		var defaultSelect = {};//{"21":"21_25","22":"22_28","23":"23_30"}
		var tmp_make_gid=allItems[0].itemid;
		var tmp_idPrefix="#make_"+tmp_make_gid+"_";
		if(!$.isEmptyObject(sellProps)){//当有规格时
			sellProperty[tmp_make_gid] = new spec_SellProperty(tmp_idPrefix,sellProps, allItems, function(argument) {
				var way = argument.way || 1;
				var pvSelected = argument.selected || false;
				var pvid = argument.pvid || '';
				var currentPV = argument.currentPV || '';
				var name = argument.name || '';
				var itemMealPvid = pvid;
				var defaultItemId=argument.defaultItemId;
				var tmp_pvid=pvid.split("_");
				var ark_spec_value=$(this.idPrefix+"spec_value_"+tmp_pvid[0]+"[way!='3']");
				if(ark_spec_value[0]){
					if(pvSelected){
						ark_spec_value.html(name);
					}else{
						ark_spec_value.html(ark_spec_value.attr("tip"));
					}
					if(way!=1){ark_spec_value.parent().click();}
				}
				//默认值获取
				if ($(this.idPrefix+"info-hx")[0]) {
					if (this.isNoPVSelected()) {
						var msg = this.getAllPVNames().join('", "');
						$(this.idPrefix+"info-hx").html(lodo_lang_js.make.tip_7+' "' + msg + '"');
					} else {
						var msg = this.getSelectedValueNames().join('", "');
						$(this.idPrefix+"info-hx").html(lodo_lang_js.make.tip_8+'<em>"'+ msg +'"</em>');
					}
				}
				$("#make_addToMealIndicator").hide();
			});
			sellProperty[tmp_make_gid].setDefaultItemId(tmp_make_gid);
			sellProperty[tmp_make_gid].setDefaultSelect(defaultSelect);
			sellProperty[tmp_make_gid].setSellPropsCount(items.length)//商品规格数量
			sellProperty[tmp_make_gid].initload();
		}else{
			$(tmp_idPrefix+"price1").hide();
			$(tmp_idPrefix+"price2").show();
			var lodo_goods_quantity=$(tmp_idPrefix+"buy_maxCount").val();
			if(lodo_goods_quantity!=-1){
				if(lodo_goods_quantity<=0){
					$(tmp_idPrefix+"stock>em").html(lodo_lang_js.make.tip_6);
					$(tmp_idPrefix+"stock>span").hide();
					$(tmp_idPrefix+"addToCart,"+tmp_idPrefix+"nowbuyToCart").hide();
					$(tmp_idPrefix+"noAddToCart").show();
				}else{
					$(tmp_idPrefix+"stock>em").html(lodo_goods_quantity);
					$(tmp_idPrefix+"stock>span").show();
					$(tmp_idPrefix+"addToCart,"+tmp_idPrefix+"nowbuyToCart").show();
					$(tmp_idPrefix+"noAddToCart").hide();
				}
			}else{
				$(tmp_idPrefix+"stock>em").html(lodo_lang_js.make.tip_16);
				$(tmp_idPrefix+"stock>span").hide();
			}
		}
	});

	Make_Meal();
}


function addtomake(gid,oaid,buyqn,typ){
	var goodsdynamic=$("#goodsdynamic");
	if(!goodsdynamic[0]){goodsdynamic=$("#make_"+gid+"_goodsdynamic");}
	var gprice=goodsdynamic.attr("gprice");
	
	var make_goods_pic=$("#make_goods_pic");
	if(!make_goods_pic[0]){make_goods_pic=$("#make_"+gid+"_goods_pic");}
	var gpicaddr=make_goods_pic.val();
	if($("#make_"+gid+"_goodsname")[0]){productName=$("#make_"+gid+"_goodsname").val();}
	var specitems=[];
	var tmp_sellProperty=sellProperty[gid];
	if(typ=="tc"){tmp_sellProperty=tc_sellProperty[gid];}
	if(tmp_sellProperty&&oaid!=""){
		for(var A in tmp_sellProperty.sellProps){
			if((""+tmp_sellProperty.selectCPID+"").indexOf(""+A+"")!=-1){
				var C=tmp_sellProperty.selectPVID[A];
				var s_name=tmp_sellProperty.sellProps[A].name;
				var s_value=tmp_sellProperty.sellProps[A].pvs[C].name;
				specitems.push({id:A+"_"+C,name:s_name,value:s_value});
			}
		}
	}
	lodo_make_goods_sel_info={
	   id:gid,
	   oaid:oaid,
	   name:productName,
	   price:gprice,
	   picaddr:gpicaddr,
	   packed:true,
	   specitems:specitems
	};
	pack.addToMakeList();
}